//
//  YDQuestionController.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2017/11/13.
//  Copyright © 2017年 LK-IOS. All rights reserved.
//

#import "YDQuestionController.h"
#import "YDQuestionHeaderModel.h"
#import "YDQuestionCellModel.h"
#import "YDQuestionTableHeaderView.h"
#import "YDQuestionTableCell.h"
#import "YDQuestionHeaderView.h"


#import <WebKit/WebKit.h>

@interface YDQuestionController ()<UITableViewDataSource , UITableViewDelegate , SYQuestionTableHeaderViewDelegate , SYQuestionHeaderViewDelegate , WKNavigationDelegate>


@property (nonatomic , weak)UIProgressView *progressView;
@property (nonatomic , weak) WKWebView *webView;

//模型数组
@property (nonatomic , strong) NSMutableArray *modelArray;
//tableView
@property (nonatomic , weak) UITableView *tableView;

@property (nonatomic, weak) YDQuestionHeaderView *headView;

@property (nonatomic, assign)Que_Type queType;

@end

@implementation YDQuestionController

-(NSMutableArray *)modelArray
{
    if (_modelArray == nil) {
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
//    self.extendedLayoutIncludesOpaqueBars = YES;
//    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = NumberColor(247, 247, 247);
    self.title = @"常见问题";
    
    //加载本地数据
//    [self configurationWithType:Que_Stock];
//
////    [self headView];
//    [self tableView];
    
    
    [self progressView];
    [self addWebView];
    
    
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
    
}
- (UIProgressView *)progressView
{
    if (!_progressView){
        UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, LKScreenW, 0)];
        progressView.tintColor = NumberColor(179, 84, 52);
        progressView.trackTintColor = NumberColor(179, 84, 52);
        [self.view addSubview:progressView];
        self.progressView = progressView;
    }
    return _progressView;
}

-(void)addWebView
{
    WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
    webView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    webView.backgroundColor = [UIColor whiteColor];
    webView.navigationDelegate = self;
    
    /*! 适应屏幕 */
    //        wkWebView.scalesPageToFit = YES;
    /*! 解决iOS9.2以上黑边问题 */
    webView.opaque = NO;
    /*! 关闭多点触控 */
    webView.multipleTouchEnabled = YES;
    /*! 加载网页中的电话号码，单击可以拨打 */
    //        wkWebView.dataDetectorTypes = YES;
    
    [self.view insertSubview:webView belowSubview:_progressView];
    
    [webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    
    NSURL *baseURL = [NSURL URLWithString:@"http://www.edt.cn/buy/new/apply.html"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:baseURL];
    
    [webView loadRequest:request];
    
    self.webView = webView;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (object == self.webView && [keyPath isEqualToString:@"estimatedProgress"])
    {
        CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
        if (newprogress == 1)
        {
            self.progressView.hidden = YES;
            [self.progressView setProgress:0 animated:NO];
        }
        else
        {
            self.progressView.hidden = NO;
            [self.progressView setProgress:newprogress animated:YES];
        }
    }
}


/***********SYQuestionHeaderViewDelegate***************/
-(void)questionHeaderView:(YDQuestionHeaderView *)headerView didClickButtonTag:(NSInteger)buttonTag
{
    if (buttonTag == 0) {
        self.queType = Que_Stock;
    }else{
        self.queType = Que_Options;
        
    }
    
//    [self configurationWithType:self.queType];
    
    [self configurationLocalData];
    
    [self.tableView reloadData];
}
-(void)configurationLocalData
{
    NSArray *array = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"OptionsQueList.plist" ofType:nil]];
    
    self.modelArray = [YDQuestionHeaderModel mj_objectArrayWithKeyValuesArray:array];
}
- (void)configurationWithType:(Que_Type)type
{
    [self.modelArray removeAllObjects];
    
    
    if (type == Que_Stock) {
        NSArray *array = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"QuestionList.plist" ofType:nil]];
        
        self.modelArray = [YDQuestionHeaderModel mj_objectArrayWithKeyValuesArray:array];
    }else
    {
        NSArray *array = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"OptionsQueList.plist" ofType:nil]];
        
        self.modelArray = [YDQuestionHeaderModel mj_objectArrayWithKeyValuesArray:array];
    }
}
//-(YDQuestionHeaderView *)headView
//{
//    if (_headView == nil) {
//        YDQuestionHeaderView *headView = [[YDQuestionHeaderView alloc] initWithFrame:CGRectMake(0, 0, LKScreenW, isPhoneX ? rateX812(40) : rate750(40))];
//        self.tableView.tableHeaderView = headView;
//        headView.delegate = self;
//        _headView = headView;
//    }
//    return _headView;
//}

/*******tableView********/
-(UITableView *)tableView
{
    if (_tableView == nil) {
        CGRect tableFrame;
        if (isPhoneX) {
            tableFrame = CGRectMake(0, 88, LKScreenW, LKScreenH - 88);
        }else{
            tableFrame = CGRectMake(0, 64, LKScreenW, LKScreenH - 64);
        }
        UITableView *tableView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStyleGrouped];
        [self.view addSubview:tableView];
        tableView.backgroundColor = NumberColor(247, 247, 247);
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.estimatedRowHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.estimatedSectionHeaderHeight = 0;
        if (isIos11) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView = tableView;
    }
    return _tableView;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.modelArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    YDQuestionHeaderModel *model = self.modelArray[section];
    
    return model.opened ? model.details.count : 0;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YDQuestionHeaderModel *model = self.modelArray[indexPath.section];
    
    YDQuestionCellModel *cellModel = model.details[indexPath.row];
    return cellModel.cellH;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return isPhoneX ? rateX812(10) : rate750(10);
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    YDQuestionHeaderModel *model = self.modelArray[section];
    return model.headH;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    YDQuestionTableHeaderView *headerView = [YDQuestionTableHeaderView headerViewWithTableView:tableView];
    headerView.delegate = self;
    YDQuestionHeaderModel *model = self.modelArray[section];
    headerView.model = model;
    return headerView;
}
/**********PZQuestionTableHeaderViewDelegate*********/
-(void)questionTableHeaderViewContentButtonClick:(YDQuestionTableHeaderView *)headerView
{
    [self.tableView reloadData];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (self.queType == Que_Stock) {
//        YDQuestionTableCell *cell = [YDQuestionTableCell cellWithTableView:tableView];
//        YDQuestionHeaderModel *model = self.modelArray[indexPath.section];
//        cell.model = model.details[indexPath.item];
//        return cell;
//    }
//    else
//    {
//        YDQuestionTableCell *cell = [YDQuestionTableCell cellWithTableView:tableView];
//        YDQuestionHeaderModel *model = self.modelArray[indexPath.section];
//        cell.model = model.details[indexPath.item];
//        return cell;
//    }
    
    YDQuestionTableCell *cell = [YDQuestionTableCell cellWithTableView:tableView];
    YDQuestionHeaderModel *model = self.modelArray[indexPath.section];
    cell.model = model.details[indexPath.item];
    return cell;
    
    
}

@end
